package com.findviewbydream.evil.helloyouth.View.Dialog.CustomDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.widget.Space;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.findviewbydream.evil.helloyouth.R;

/********************************************
 * 工程名：HelloYouth
 * 包名：com.findviewbydream.evil.helloyouth.Utils.Dialog.CustomDialog
 * 时间：16-6-11 上午12:43
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
public class Builder
{
    private CustomDialog dialog;
    private View view;

    private TextView title_TextView;
    private LinearLayout linearLayout_title;
    private TextView message_TextView;
    private LinearLayout linearLayout_message;
    private Button positiveButton;
    private Space neutralButton_space;
    private Button neutralButton;
    private Space negativeButton_space;
    private Button negativeButton;
    private RelativeLayout relativeLayout;
    private CardView cardView;

    private String title;
    private String message;
    private View contentView;
    private int titleSize = 0;
    private int messageSize = 0;
    private boolean dialogDismiss = false;

    private DialogInterface.OnClickListener positiveListener;
    private DialogInterface.OnClickListener neutralListener;
    private DialogInterface.OnClickListener negativeListener;

    private String positiveContent;
    private String neutralContent;
    private String negativeContent;


    public Builder(Context context)
    {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        dialog = new CustomDialog(context, R.style.Dialog);

        view = inflater.inflate(R.layout.dialog, null);

        dialog.addContentView(view, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        initView(view);
    }

    private void initView(View view)
    {
        title_TextView = (TextView) view.findViewById(R.id.dialog_title);
        linearLayout_title = (LinearLayout) view.findViewById(R.id.dialog_linearLayout_title);
        message_TextView = (TextView) view.findViewById(R.id.dialog_message);
        linearLayout_message = (LinearLayout) view.findViewById(R.id.dialog_linearLayout_message);
        positiveButton = (Button) view.findViewById(R.id.dialog_positiveButton);
        neutralButton_space = (Space) view.findViewById(R.id.dialog_neutralButton_space);
        neutralButton = (Button) view.findViewById(R.id.dialog_neutralButton);
        negativeButton_space = (Space) view.findViewById(R.id.dialog_negativeButton_space);
        negativeButton = (Button) view.findViewById(R.id.dialog_negativeButton);
        relativeLayout = (RelativeLayout) view.findViewById(R.id.dialog_relativeLayout);
        cardView = (CardView) view.findViewById(R.id.dialog_cardView);
    }

    public Builder setTitle(String title)
    {
        this.title = title;
        return this;
    }

    public Builder setTitleSize(int titleSize)
    {
        this.titleSize = titleSize;
        return this;
    }


    public Builder setMessage(String message)
    {
        this.message = message;
        return this;
    }

    public Builder setMessageSize(int messageSize)
    {
        this.messageSize = messageSize;
        return this;
    }

    public Builder setContentView(View contentView)
    {
        this.contentView = contentView;
        return this;
    }

    public Builder setPositiveButton(String positiveContent, DialogInterface.OnClickListener positiveListener)
    {
        this.positiveListener = positiveListener;
        this.positiveContent = positiveContent;
        return this;
    }

    public Builder setNeutralButton(String neutralContent, DialogInterface.OnClickListener neutralListener)
    {
        this.neutralListener = neutralListener;
        this.neutralContent = neutralContent;
        return this;
    }


    public Builder setNegativeButton(String negativeContent, DialogInterface.OnClickListener negativeListener)
    {
        this.negativeListener = negativeListener;
        this.negativeContent = negativeContent;
        return this;
    }

    public Builder setCanceledOnTouchOutside(boolean b)
    {
        dialog.setCanceledOnTouchOutside(b);
        return this;
    }

    public Builder setDialogDismiss(boolean b)
    {
        this.dialogDismiss = b;
        return this;
    }


    public CustomDialog show()
    {
        /*头部*/
        if (title != null)
        {
            title_TextView.setText(title);
            if (titleSize != 0)
                title_TextView.setTextSize(titleSize);
        }
        else
            linearLayout_title.setVisibility(View.GONE);

        /*中部*/
        if (message != null)
        {
            message_TextView.setText(message);
            if (messageSize != 0)
                message_TextView.setTextSize(messageSize);
        }
        else if (contentView != null)
        {
            linearLayout_message.removeAllViews();
            linearLayout_message.addView(contentView
                    , new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                            , ViewGroup.LayoutParams.MATCH_PARENT));
        }

        /*按钮部*/
        if (positiveListener != null)
        {
            positiveButton.setVisibility(View.VISIBLE);
            positiveButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    if (dialogDismiss)
                        dialog.dismiss();
                    positiveListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                }
            });
            positiveButton.setText(positiveContent);
        }
        if (neutralListener != null)
        {
            neutralButton.setVisibility(View.VISIBLE);
            neutralButton_space.setVisibility(View.VISIBLE);
            neutralButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    dialog.dismiss();
                    neutralListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                }
            });
            neutralButton.setText(neutralContent);
        }

        if (negativeListener != null)
        {
            negativeButton.setVisibility(View.VISIBLE);
            negativeButton_space.setVisibility(View.VISIBLE);
            negativeButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    dialog.dismiss();
                    negativeListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                }
            });
            negativeButton.setText(negativeContent);
        }

        dialog.show();
        return dialog;
    }
}
